/**
 * @file common.js
 */
var tousu = {
    // 记录最新提交表单时所有link的id,
    tempLinksId: [],
    cutTabCbk: function () {
    },
    // 初始化时需要隐tabindex, 格式如：[num, num] 或 num
    hidTabIndex: null,
    init: function () {
        // tabIndex值(页面中从1开始，setTab中从0开始计数)
        var tabIndex = location.hash ? (location.hash.replace('#', '') * 1 - 1) : 0;
        var cutTabCbk = window['cutTabCbk'] || this.cutTabCbk;
        var hideIndex = window['hidTabIndex'] || this.hidTabIndex;
        var initTab = function (showIndex) {
            /* globals SetTab */
            var cutTab = new SetTab({
                tabWrapId: 'formTab',
                cntWrapId: 'form-panel',
                showIndex: showIndex,
                eCallback: cutTabCbk
            });
        };
        if (hideIndex == null) {
            initTab(tabIndex);
        } else {
            var tabMenus = baidu('#formTab').children();
            var tabMenuLen = tabMenus.length;
            if ((typeof hideIndex).toLowerCase() === 'number') {
                hideIndex = [hideIndex];
            }
            if (tabMenuLen > hideIndex.length) {
                var strHidIndex = ',' + hideIndex.join(',') + ',';
                if (strHidIndex.indexOf(',' + tabIndex + ',') > -1) {
                    tabIndex = -1;
                }
                for (var i = 0; i < tabMenuLen; i++) {
                    var tmpIndex = ',' + i + ',';
                    var hidPos = strHidIndex.indexOf(',' + i + ',');
                    if (hidPos > -1) {
                        hideIndex.splice(hidPos, 1);
                        strHidIndex = ',' + hideIndex.join(',') + ',';
                    } else {
                        if (tabIndex === -1) {
                            tabIndex = i;
                            break;
                        }
                    }
                }
            }
            initTab(tabIndex);
        }
        this.setTextArea();
    },
    initLogin: function () {
        /* globals is_login */
        if (is_login !== '1') {
            /* globals login */
            login.init(baidu('#form-panel').find('a.btnLog'));
        }
    },
    setTextArea: function () {
        var t = this;
        $('.ipt-area,.iptxt').focus(function () {
            var t = this;
            $(t).css({color: '#666'});
        });
        $('.ipt-area,.iptxt').blur(function () {
            var t = this;
            $(t).css({color: '#aaa'});
        });
    },
    addLink: function (linksWrapId, addNum, maxNum, callback) {
        var linkWrap = baidu('#' + linksWrapId);
        var startNum = linkWrap.find('input.formlink').length;
        if (!maxNum || (maxNum < 0 || maxNum > 12)) {
            maxNum = 12;
        }
        if (startNum >= maxNum) {
            return;
        }
        var htm = [];
        if (!addNum || addNum <= 0) {
            addNum = 1;
        }
        var stopNum = startNum + addNum;
        if (stopNum > maxNum) {
            stopNum = maxNum;
        }
        // 增加的input id
        var appendIptId = [];
        for (var i = startNum + 1; i <= stopNum; i++) {
            appendIptId.push(linksWrapId + i);
            var tempHtm = '<div><input id="' + linksWrapId + i;
            tempHtm += '" type="text" value="" class="iptxt formlink" size="40" maxlength="1024"/> ';
            tempHtm += '<span id="' + linksWrapId + i + '_ok">';
            tempHtm += '</span> <p id="' + linksWrapId + i + '_err" class="err"></p></div>';
            htm.push(tempHtm);
        }
        linkWrap.append(htm.join(''));
        if (stopNum === maxNum) {
            // Modified by Linyu 20150930
            // baidu('#' + linksWrapId + '_add').attr('disabled', 'disabled');
            // baidu('#' + linksWrapId + '_add').attr('class', 'btn1 link-gray');
            $('#' + linksWrapId + '_add').parent().addClass('disabled');
        }
        if (callback) {
            callback(appendIptId);
        }
    },
    // 提示有重复的link
    // repeat_links: 服务端返回的重复的link的索引值,即提交的第n个link是重复的
    fixedRepeatLink: function (formIdNum, repeatLinks) {
        /* globals vaildForm */
        var formMsg = vaildForm.msg;
        for (var i = 0; i < repeatLinks.length; i++) {
            vaildForm.setChkMsg(this.tempLinksId[repeatLinks[i] - 1], formMsg.linkRepeat);
        }
    }
};

baidu.dom(document).ready(function () {
    tousu.init();
});